﻿namespace IBTrader.Indicator
{
    using IBTrader.Charts;
    using IBTrader.Modules.Read;
    using System;
    using System.Collections.Generic;

    abstract class Base : IIndicator
    {
        public event EventHandler<Line> Hit;
        protected readonly Dictionary<ChartArgs.EventType, IChartArgs> chartArgs = new Dictionary<ChartArgs.EventType, IChartArgs>();
        protected Base(Worker worker, IChartArgs chartArgs)
        {
            if (worker != null) worker.Filters.Add(this);
            this.chartArgs.Add(ChartArgs.EventType.Hit, chartArgs);
        }
        protected Base(Worker worker) : this(worker, new ChartArgs()) { }
        public abstract void Add(object sender, Line line);
        protected void OnHit(Line line)
        {
            OnHit(Hit, ChartArgs.EventType.Hit, line);
        }
        protected virtual void OnHit(Line line, double price)
        {
            OnHit(NewLine(line, price));
        }
        protected void OnHit(EventHandler<Line> handler, ChartArgs.EventType type, Line line, double price)
        {
            OnHit(handler,type, NewLine(line, price));
        }
        protected void OnHit(EventHandler<Line> handler, ChartArgs.EventType type, Line line)
        {
            if (handler != null) handler(chartArgs[type], line);
        }
        internal static Func<double, double> NaN = d => double.IsNaN(d) || double.IsInfinity(d) ? 0 : d;
        internal static Func<Line,double,Line> NewLine = (line, price) => new Line { DateTime = line.DateTime, Price = price };
    }
}